#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"

/********************************************************************************************
nt37700c 639 Dante
********************************************************************************************
********************************************************************************************/

void NT37700_Enable_Flash_Control_and_SPI_Flash_En(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	//u16 tmp=0;
	//u8 buffer1[3];	

	NT37700C_QE1(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
	
	delay_ms(5);
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}
void NT37700_Inter_Demura_Ram(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	//u8 buffer1[3];

	NT37700C_Program_Flash_Set(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);

	buffer[0] = 0x2F;
	buffer[1] = 0x01;		

	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);        																					
}
                                    
void NT37700_Exit_Demura_Ram(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}
                               
void NT37700_Flash_Write_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		//u32 error_times=0;
		//u8 buffer1[3];
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15);
		
		NT37700C_Program_Flash_Load(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);
		
		buffer[0] = 0x2F;
		buffer[1] = 0x03;		
		SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
		STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);		
}                       

void NT37700_Flash_Write_Demura2(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}


void NT37700_Flash_Check_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 PAGE4[7] = {0x06,0xF0,0x55,0xAA,0x52,0x08,0x04};
	u8 REG_D0[7] = {0x06,0xD0,0x00,0x7F,0x00,0x00,0x84};
	u8 buffer1[8]; 
	u16 tmp;

	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	
	buffer1[0] = 0x01; 
	buffer1[1] = 0x10; 			
	SSD2828_W_Array(SigMode,channel,buffer1,0);			
	buffer1[0] = 0x01; 
	buffer1[1] = 0x28; 			
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	SSD2828_W_Array(SigMode,channel,PAGE4,0);
	buffer1[0] = 0x02; 
	buffer1[1] = 0xc2; 
	buffer1[2] = 0x54; 		
	SSD2828_W_Array(SigMode,channel,buffer1,0);	
		buffer1[0] = 0x03; 
	  buffer1[1] = 0xB1; 
	  buffer1[2] = 0x01; 	
	  buffer1[3] = 0xC1; 		
	  SSD2828_W_Array(SigMode,channel,buffer1,0);		
		buffer1[0] = 0x02; 
	  buffer1[1] = 0x6F; 
	  buffer1[2] = 0x09; 		
	  SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x05; 
	  buffer1[1] = 0xB1; 
	  buffer1[2] = 0xf0; 	
	  buffer1[3] = 0x47; 		
	  buffer1[4] = 0x13; 	
	  buffer1[5] = 0x00; 			
	  SSD2828_W_Array(SigMode,channel,buffer1,0);	
		SSD2828_W_Array(SigMode,channel,REG_D0,0);
//--------------------------------------------------------		
		buffer1[0] = 0x01; 
	  buffer1[1] = 0x11; 			
	  SSD2828_W_Array(SigMode,channel,buffer1,0);		
		
//SETSRAMWADDR 	0x2C	
//SETSRAMCWADDR	0x3C	
//		
//SETSRAMRADDR	0x2E	
//SETSRAMCRADDR	0x3E	
//BMPLD	"D:\MY_Project\NT37700C\GVO\Demura\Demura\L64.bmp"			
			
		delay_ms(150);	
		buffer1[0] = 0x01; 
	  buffer1[1] = 0x29; 			
	  SSD2828_W_Array(SigMode,channel,buffer1,0);		
		delay_ms(120);				

		SSD2828_W_Array(SigMode,channel,PAGE4,0);
		buffer1[0] = 0x02; 
	  buffer1[1] = 0x6F; 
	  buffer1[2] = 0x02; 		
	  SSD2828_W_Array(SigMode,channel,buffer1,0);
		
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080); 
		SSD2828_W_Reg(SigMode,channel,0xBC,0x0001); 
		SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); //return package  
					 
		buffer1[0] = 0x01;
		buffer1[1] = 0xC2;
		SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(6);
		tmp=SSD2828_R_Reg(SigMode,channel,0xFF);
		buffer[4]=tmp;
		buffer[5]=tmp>>8;
		delay_ms(5);

	buffer[0] = 0x2F;
	buffer[1] = 0x04;					
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);             //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  	
}
                                  
void NT37700_Flash_Erase_Demura(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	//u8 buffer1[3];
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);
	
	NT37700C_Erase_Flash(pdev,ep_addr,SigMode,channel,buffer,LP_B7_Data,HS_B7_Data);	
	buffer[0] = 0x2F;
	buffer[1] = 0x05;		
	buffer[4] = 0x00;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú	
}

void NT37700_Flash_Demura_OTP(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
;
}

void NT37700_Demura_Function_ON(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);	
	
	buffer1[0] = 0x06;
	buffer1[1] = 0xF0;
	buffer1[2] = 0x55;
	buffer1[3] = 0xAA;
	buffer1[4] = 0x52;
	buffer1[5] = 0x08;
	buffer1[6] = 0x04;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);	
	buffer1[0] = 0x06;
	buffer1[1] = 0xD0;
	buffer1[2] = 0x00;
	buffer1[3] = 0x7F;
	buffer1[4] = 0x00;
	buffer1[5] = 0x00;
	buffer1[6] = 0x84; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);
	buffer[0] = 0x2F;
	buffer[1] = 0x07;		
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //êy?Y3¤?è-crcá?×??ú
}

void NT37700_Demura_Function_OFF(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	u8 buffer1[7];	
	SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
	delay_ms(15);

	buffer1[0] = 0x06;
	buffer1[1] = 0xF0;
	buffer1[2] = 0x55;
	buffer1[3] = 0xAA;
	buffer1[4] = 0x52;
	buffer1[5] = 0x08;
	buffer1[6] = 0x04;
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	delay_ms(5);		
	buffer1[0] = 0x06;
	buffer1[1] = 0xD0;
	buffer1[2] = 0x00;
	buffer1[3] = 0x7F;
	buffer1[4] = 0x00;
	buffer1[5] = 0x00;
	buffer1[6] = 0x00; 
	SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	delay_ms(5);
	buffer[0] = 0x2F;  //Demura_type
	buffer[1] = 0x08;				
	SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //·μ?? ?áè?μ? Gammaêy?Y  ￡ook  
}

void ProcessForDmuR07( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	switch(USB_Rx_Buffer[1])
	{
			case 0x01:                                      //写寄存器
					NT37700_Inter_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x02:                                      //读寄存器
					NT37700_Exit_Demura_Ram(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;						 
			case 0x03:                                      //读寄存器
					NT37700_Flash_Write_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x04:                                      //读寄存器
					NT37700_Flash_Check_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;												
			case 0x05:                                      //读寄存器
					NT37700_Flash_Erase_Demura(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			case 0x06:                                      //check flash ID
					NT37700_Enable_Flash_Control_and_SPI_Flash_En(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x07:                                      //check flash ID
					NT37700_Demura_Function_ON(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;	
			case 0x08:                                      //check flash ID
					NT37700_Demura_Function_OFF(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;													
			case 0x0A:
					NT37700_Flash_Write_Demura2(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);
			break;
			default:
				break;												
	}
}
//********************************************************************************************